/*
 * @Description:
 * @Author: fhw
 * @Date: 2023-02-17 13:58:56
 */
import {
  Body,
  Controller,
  HttpCode,
  Post,
  Headers,
  Get,
  Query,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import {
  ApiQuery,
  ApiBody,
  ApiOperation,
  ApiResponse,
  ApiTags,
  ApiBearerAuth,
} from '@nestjs/swagger';
import { Roles } from 'src/common/auth/role.decorator';
import { throwHttp } from 'src/utlis';
import { isTrue } from 'src/utlis/tools';
import { RoleEntity } from '../role/entities/role.entity';
import { AdminUserService } from './admin-user.service';
import {
  UpdatePassWordDto,
  ValidateUserDto,
  RegisterUserDto,
  DelteIdDto,
  AdminUserBindRoleDto,
  GetAdminUserPage,
  UpdateAdminUserDto,
} from './dto/admin-user.dto';
import { AdminUser } from './entities/admin-user.entity';
@ApiBearerAuth() // swagger文档设置token
@ApiTags('后台管理用户')
@Controller('admin-user')
export class AdminUserController {
  constructor(
    private readonly adminUserService: AdminUserService,
    private readonly jwtService: JwtService,
  ) { }
  /** 后台用户列表 */
  @HttpCode(200)
  @Get('/getAdminUserPage')
  @ApiResponse({
    status: 200,
    description: '后台用户列表',
    type: [AdminUser],
  })
  @ApiOperation({
    tags: ['获取后台用户列表'],
    summary: '获取后台用户列表',
    operationId: 'getAdminUserPage',
  })
  @Roles()
  getAdminUserPage(@Query() params: GetAdminUserPage) {
    return this.adminUserService.getAdminUserPage(params);
  }
  @HttpCode(200)
  @Post('/registerAdminUser')
  @ApiQuery({
    description: '用户注册',
    type: RegisterUserDto,
  })
  @ApiBody({ description: '用户注册', type: RegisterUserDto })
  @ApiOperation({ summary: '用户注册', operationId: 'registerAdminUser' })
  @ApiResponse({ status: 200, type: AdminUser })
  registerAdminUser(@Body() body: RegisterUserDto) {
    return this.adminUserService.registerAdminUser(body);
  }
  @HttpCode(200)
  @Post('/login')
  @ApiQuery({
    description: '登录',
    type: ValidateUserDto,
  })
  @ApiBody({ description: '登录', type: ValidateUserDto })
  @ApiOperation({ summary: '登录', operationId: 'login' })
  login(@Body() body: ValidateUserDto) {
    try {
      return this.adminUserService.login(body);
    } catch (error) {
      throwHttp(error.message);
    }
  }
  /** 用户详情 */
  @HttpCode(200)
  @Get('/getUserInfo')
  @ApiResponse({
    status: 200,
    description: '用户详情',
    type: AdminUser,
  })
  @ApiOperation({
    tags: ['获取用户详情'],
    summary: '获取用户详情',
    operationId: 'getUserInfo',
  })
  @Roles()
  async getUserInfo(@Headers() headers) {
    const token = headers.authorization.split(' ');
    if (!isTrue(token)) {
      throwHttp('获取token失败');
    }
    const user: any = this.jwtService.decode(token[1]);
    const data = await this.adminUserService.getUserInfo({
      id: user.userId,
    });
    data.roles = data.roles?.rolename as any
    return data;
  }
  @HttpCode(200)
  @Post('/updatePassWord')
  @ApiQuery({
    description: '修改密码',
    type: UpdatePassWordDto,
  })
  @ApiBody({ description: '修改密码', type: UpdatePassWordDto })
  @ApiOperation({ summary: '修改密码', operationId: 'updatePassWord' })
  @Roles('admin') // 自定义装璜器
  @ApiResponse({ status: 200, type: String })
  updatePassWord(@Body() body: UpdatePassWordDto) {
    return this.adminUserService.updatePassWord(body);
  }
  @HttpCode(200)
  @Post('/resetPassWord')
  @ApiQuery({
    description: '重置密码',
    type: DelteIdDto,
  })
  @ApiBody({ description: '重置密码', type: DelteIdDto })
  @ApiOperation({ summary: '重置密码', operationId: 'resetPassWord' })
  @Roles('admin') // 自定义装璜器
  @ApiResponse({ status: 200, type: String })
  resetPassWord(@Body() body: DelteIdDto) {
    return this.adminUserService.resetPassWord(body);
  }

  @HttpCode(200)
  @Post('/delAdminUser')
  @ApiQuery({
    description: '删除用户',
    type: DelteIdDto,
  })
  @ApiBody({ description: '删除用户', type: DelteIdDto })
  @ApiOperation({ summary: '删除用户', operationId: 'delAdminUser' })
  @Roles('admin') // 自定义装璜器
  delAdminUser(@Body() body: DelteIdDto) {
    return this.adminUserService.delAdminUser(body);
  }
  /** 用户角色 */
  @HttpCode(200)
  @Get('/getUserRole')
  @ApiResponse({
    status: 200,
    description: '用户角色',
    type: [RoleEntity],
  })
  @ApiOperation({
    tags: ['获取用户角色'],
    summary: '获取用户角色',
    operationId: 'getUserRole',
  })
  getUserRole(@Headers() headers) {
    return this.adminUserService.getUserRole(headers);
  }
  @HttpCode(200)
  @Post('/adminUserBindRole')
  @ApiQuery({
    description: '绑定用户角色',
    type: AdminUserBindRoleDto,
  })
  @ApiBody({ description: '绑定用户角色', type: AdminUserBindRoleDto })
  @ApiOperation({ summary: '绑定用户角色', operationId: 'adminUserBindRole' })
  @Roles('admin') // 自定义装璜器
  adminUserBindRole(@Body() body: AdminUserBindRoleDto) {
    return this.adminUserService.adminUserBindRole(body);
  }
  @HttpCode(200)
  @Post('/updateAdminUser')
  @ApiQuery({
    description: '更新用户信息',
    type: UpdateAdminUserDto,
  })
  @ApiBody({ description: '更新用户信息', type: UpdateAdminUserDto })
  @ApiOperation({ summary: '更新用户信息', operationId: 'updateAdminUser' })
  @Roles('admin') // 自定义装璜器
  updateAdminUser(@Body() body: UpdateAdminUserDto) {
    return this.adminUserService.updateAdminUser(body);
  }
}
